package com.music.action.authority;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.music.action.BaseAction;
import com.music.action.BaseKey;
import com.music.authority.service.UpMusicService;
import com.music.common.ParamUtil;
import com.music.domain.CateMusic;

/**
 * 音乐上传action
 * 
 * @author LFSenior
 *
 */
public class UpMusicAction extends BaseAction {
	@Autowired
	UpMusicService upMusicService;
	
	private String name;
	private String author;
	private String musicFileName;
	private File music;
	private String musicContentType;
	private String cate;
	private File image;
	private String imageFileName;
	private String imageContentType;

	/**
	 * 跳转到上传音乐界面
	 * 
	 * @return
	 */
	@Action(value = "upmusic", results = { @Result(name = "upmusic", location = "authority/upmusic.jsp") })
	public String redicterUpMusic(){
		request.put(BaseKey.AUTHORITY_MUSIC_LIST, upMusicService.findAllMusic());
		return "upmusic";
	}
	
	@Action(value="delMusic",results={@Result(name=POST,type="json",params={"root","reParam"})})
	public String delMusic(){
		try {
			Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
			upMusicService.deleteMusic(param.get("musicId").toString());
			reParam.put("result", "T");
		} catch (Exception e) {
			reParam.put("result", "F");
		}
		return POST;
	}

	/**
	 * 跳转到音乐表单提交
	 * 
	 * @return
	 */

	@Action(value = "frommusic", results = { @Result(name = "frommusic", location = "authority/frommusic.jsp") })
	public String redicterFromMusic() {
		return "frommusic";
	}

	
	
	/**
	 * 加载类别
	 */
	@Action(value="loadCate",results={@Result(name=POST,type="json",params={"root","reParam"})})
	public String loadCate(){
		try {
			List<CateMusic> cates=upMusicService.findAllCate();
			reParam.put("data", cates);
			reParam.put("state", "T");
		} catch (Exception e) {
			reParam.put("state", "F");
		}
		return POST;
	}
	
	
	/**
	 * 上传文件
	 * @return
	 */
	@Action(value="uploadmusic",results={@Result(name="success",location="authority/upmusic.jsp"),@Result(name="fail",location="authority/uperror.jsp")})
	public String uploadMusicFile(){
		InputStream is=null;  
        OutputStream os=null;  
        File file=null;
        String musicId=ParamUtil.buildUUID();
        String musicBaseURI=ServletActionContext.getServletContext().getRealPath("\\")+"\\upload\\";
        String saveMusic=musicBaseURI+musicId+musicFileName;
        String musicURI=ServletActionContext.getServletContext().getContextPath()+"/upload/"+musicId+musicFileName;
        String imageBaseURI=ServletActionContext.getServletContext().getRealPath("\\")+"\\image\\";
        String imageURI="\\image\\"+imageFileName;
        String saveImage=ServletActionContext.getServletContext().getRealPath("\\")+imageURI;
        Map param=new HashMap<>();
        param.put("id", musicId);
        param.put("musicURI", musicURI);
        param.put("name", name);
        param.put("author", author);
        param.put("cateid", cate);
        try {  
            is = new BufferedInputStream(new FileInputStream(music));
            file=new File(musicBaseURI);
            file.mkdirs();
            os = new BufferedOutputStream(new FileOutputStream(saveMusic));  
            byte[] buffer = new byte[1024];  
            int len=0;  
            while((len=is.read(buffer))>0){  
                os.write(buffer,0,len);  
            }   
            if(image!=null){
            	 is=new BufferedInputStream(new FileInputStream(image));
            	 file=new File(imageBaseURI);
            	 file.mkdirs();
            	 os=new BufferedOutputStream(new FileOutputStream(saveImage));
                 buffer = new byte[1024];
                 len=0;  
                 while((len=is.read(buffer))>0){  
                     os.write(buffer,0,len);  
                 }  
                 param.put("imageURI", imageURI);
            }else{
            	param.put("imageURI", "/static/images/p2.jpg");
            }
            /**
             * 保存
             */
            upMusicService.insertMusic(param);
        }catch (Exception e) {
        	return "fail";
		}finally{  
            if(is !=null){try {
				is.close();
			} catch (IOException e) {
				return "fail";
			}}  
            if(os!=null){try {
				os.close();
			} catch (IOException e) {
				return "fail";
			}}  
        }  
		return "success";
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getMusicFileName() {
		return musicFileName;
	}

	public void setMusicFileName(String musicFileName) {
		this.musicFileName = musicFileName;
	}

	public File getMusic() {
		return music;
	}

	public void setMusic(File music) {
		this.music = music;
	}

	public String getMusicContentType() {
		return musicContentType;
	}

	public void setMusicContentType(String musicContentType) {
		this.musicContentType = musicContentType;
	}

	public String getCate() {
		return cate;
	}

	public void setCate(String cate) {
		this.cate = cate;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}
	

}
